<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MealShop extends Model
{
    const CREATED_AT = 'insert_time';
    const UPDATED_AT = 'update_time';
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'lexiang_meal_shop';

    protected $primaryKey = 'shop_id';

    public $timestamps = false;
    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['shop_name','shop_position_x','shop_position_y','shop_qrcode','shop_sd','shop_logo','shop_slogan','shop_tel','update_time','shop_run_status'];

    public function createShop($createData,$userid)
    {
    	$nowTime = time();
        $createData['users_id'] = $userid;
    	$createData['shop_run_status'] = 1;
    	$createData['insert_time'] = $nowTime;
    	return self::insertGetId($createData);
    }

    public function controlSwitch($action,$shopid)
    {
        $nowtime = time();
        $shop = self::find($shopid);
        $shop->shop_run_status = $action;
        $shop->update_time = $nowtime;
        return $shop->save();
    }

    public function retrieShop($key,$value,$field) 
    {
        return self::where($key,$value)->select($field)->first();
    }

    /**
     * 今日排号
     */
    public static function getnumber($shopId)
    {
        $start = strtotime(date('Ymd 00:00:00'));
        $end = strtotime(date('Ymd 23:59:59'));
        $num = MealOrder::where([
            ['shops_id','=',$shopId],
            ['order_status','>',-1],
        ])->whereBetween('insert_time',[$start,$end])->orderBy('order_sernum','desc')->value('order_sernum') ?? 0;
        return ($num+1);
    }

    /**
     * 商圈
     */
    public function getCircleNameAttribute($v)
    {
        return Circle::where('circle_id',$this->circle_id)->value('circle_name') ?? '未设置';
    }

    /**
     * 金额
     */
    public function getAccountAttribute($v)
    {
        return $v;
    }

    public function getDistanceNowAttribute($v)
    {
        return get_distance($this->shop_position_x,$this->shop_position_y,$this->lat2,$this->lng2);
    }

    /**
     * logo
     */
    public function getShopLogoAttribute($v)
    {
        return 'https://jiashilexiang.oss-cn-beijing.aliyuncs.com/MealShopLogo'.$v;
    }

    /**
     * 时间
     */
    public function getInsertTimeAttribute($v)
    {
        return date('Y-m-d H:i',$v);
    }

    /**
     * 营业状态
     */
    public function getBusinessStateAttribute($v)
    {
        $nowTime = time();
        $startDate = strtotime(date('Y-m-d '.$this->start_run_time));
        $overDate = strtotime(date('Y-m-d '.$this->over_run_time));
        $iftimebettwn = ($startDate <= $nowTime) && ($overDate >= $nowTime);
        if($iftimebettwn){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 营业状态
     */
    public function getShopRunStatusTextAttribute($v)
    {
        $statusDe = [
            '-1'=>'关停',
            '0'=>'暂停',
            '1'=>'营业'
        ];
        return $statusDe[$this->shop_run_status];
    }
}
